#include "ZenCube.h"

CZenCube::CZenCube()
{
	m_Texture	= 0;
	m_pIB		= 0;
}

CZenCube::CZenCube(float Width, float Height, float Depth, D3DCOLOR Color)
{
	this->SetVertices(Width, Height, Depth, Color);
}

CZenCube::~CZenCube()
{
	if( m_pIB )
		m_pIB->Release();
}

void CZenCube::SetVertices(float Width, float Height, float Depth, D3DCOLOR Color)
{
	Width  /= 2;
	Height /= 2;
	Depth  /= 2;
	
	m_Vertices[0].m_Position.x = -Width;
	m_Vertices[0].m_Position.y =  Height;
	m_Vertices[0].m_Position.z = -Depth;

	m_Vertices[0].m_Normal.x = -1.0f;
	m_Vertices[0].m_Normal.y =  1.0f;
	m_Vertices[0].m_Normal.z = -1.0f;

	m_Vertices[0].m_DiffuseColor = Color;

	m_Vertices[1].m_Position.x =  Width;
	m_Vertices[1].m_Position.y =  Height;
	m_Vertices[1].m_Position.z = -Depth;

	m_Vertices[1].m_Normal.x =  1.0f;
	m_Vertices[1].m_Normal.y =  1.0f;
	m_Vertices[1].m_Normal.z = -1.0f;

	m_Vertices[1].m_DiffuseColor = Color;

	m_Vertices[2].m_Position.x = -Width;
	m_Vertices[2].m_Position.y = -Height;
	m_Vertices[2].m_Position.z = -Depth;

	m_Vertices[2].m_Normal.x = -1.0f;
	m_Vertices[2].m_Normal.y = -1.0f;
	m_Vertices[2].m_Normal.z = -1.0f;

	m_Vertices[2].m_DiffuseColor = Color;

	m_Vertices[3].m_Position.x =  Width;
	m_Vertices[3].m_Position.y = -Height;
	m_Vertices[3].m_Position.z = -Depth;

	m_Vertices[3].m_Normal.x =  1.0f;
	m_Vertices[3].m_Normal.y = -1.0f;
	m_Vertices[3].m_Normal.z = -1.0f;

	m_Vertices[3].m_DiffuseColor = Color;

	m_Vertices[4].m_Position.x = -Width;
	m_Vertices[4].m_Position.y =  Height;
	m_Vertices[4].m_Position.z =  Depth;

	m_Vertices[4].m_Normal.x = -1.0f;
	m_Vertices[4].m_Normal.y =  1.0f;
	m_Vertices[4].m_Normal.z =  1.0f;

	m_Vertices[4].m_DiffuseColor = Color;

	m_Vertices[5].m_Position.x =  Width;
	m_Vertices[5].m_Position.y =  Height;
	m_Vertices[5].m_Position.z =  Depth;

	m_Vertices[5].m_Normal.x =  1.0f;
	m_Vertices[5].m_Normal.y =  1.0f;
	m_Vertices[5].m_Normal.z =  1.0f;

	m_Vertices[5].m_DiffuseColor = Color;

	m_Vertices[6].m_Position.x = -Width;
	m_Vertices[6].m_Position.y = -Height;
	m_Vertices[6].m_Position.z =  Depth;

	m_Vertices[6].m_Normal.x =  1.0f;
	m_Vertices[6].m_Normal.y = -1.0f;
	m_Vertices[6].m_Normal.z =  1.0f;

	m_Vertices[6].m_DiffuseColor = Color;

	m_Vertices[7].m_Position.x =  Width;
	m_Vertices[7].m_Position.y = -Height;
	m_Vertices[7].m_Position.z =  Depth;

	m_Vertices[7].m_Normal.x =  1.0f;
	m_Vertices[7].m_Normal.y = -1.0f;
	m_Vertices[7].m_Normal.z =  1.0f;

	m_Vertices[7].m_DiffuseColor = Color;

}

HRESULT CZenCube::Render()
{
	HRESULT					res			= 0;
	BYTE *					pIndexData	= 0;
	BYTE *					pVertexData = 0;	
	LPDIRECT3DVERTEXBUFFER8 pVB			= 0;
	static BOOL				bFirst		= TRUE;

	if( bFirst )
	{
		zen.m_pDevice->CreateIndexBuffer( sizeof(short)*36, D3DUSAGE_WRITEONLY, 
									D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIB );
		
		short Indices[36] = 
		{ 
			0, 1, 2,
			1, 3, 2,
			1, 5, 3,
			3, 5, 7,
			0, 5, 1,
			0, 4, 5,
			2, 6, 7,
			2, 7, 3,
			2, 0, 4,
			2, 4, 6,
			6, 4, 5,
			6, 5, 7 
		};

		m_pIB->Lock( 0, 0, &pIndexData, 0 );
		
		CopyMemory( pIndexData, (void*)&Indices, sizeof(Indices) );

		m_pIB->Unlock();

		bFirst = FALSE;
	}

	res = zen.m_pDevice->CreateVertexBuffer( sizeof( CZenVertex )*8, D3DUSAGE_WRITEONLY, 
												ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB );
	if( FAILED( res ) )
		return E_FAIL;

	res = pVB->Lock( 0, 0, &pVertexData, 0 );
	if( FAILED( res ) )
	{
		pVB->Release();
		return E_FAIL;
	}

	CopyMemory( pVertexData, (void*)&m_Vertices, sizeof( m_Vertices ) );

	pVB->Unlock();

	zen.m_pDevice->SetStreamSource( 0, pVB, sizeof( CZenVertex ) );
	zen.m_pDevice->SetIndices( m_pIB, 0 );

	//Teken de kubus
	//Fout in het boek. Je moet niet opgeven hoeveel vertices er in totaal 
	//deel uitmaken van driehoeken, maar juist het aantal vertices in de 
	//vertex-buffer, dus het aantal UNIEKE vertices!
	if(FAILED(this->zen.m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,8,0,12)))
	{

		MessageBox(NULL,"Kan geen indexed primitive tekenen!","!",MB_TOPMOST);
		pVB->Release();
		return E_FAIL;

	}

	pVB->Release();
	return S_OK;
}
